package com.fruit.web.vo.common;

import java.sql.Timestamp;
import java.util.ArrayList;

import java.util.List;
import com.fruit.hibernate.dao.support.Page;
import com.fruit.service.ICouponService;
import com.fruit.utils.DateTimeUtil;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.bo.impl.Coupon;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.constant.SpringBeanConstants;

public class CouponPage implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224759L;
	private Page<Coupon,Long> page = new Page<Coupon,Long>();
	private Integer pageSize ;
	private Integer pageNo ;
	private Coupon param;

	public Page<Coupon, Long> getPage() {
		return page;
	}
	public void setPage(Page<Coupon, Long> page) {
		this.page = page;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
	public Coupon getParam() {
		return param;
	}
	public void setParam(Coupon param) {
		this.param = param;
	}
	
	public CouponPage(){
	}
	
	public CouponPage(Integer pageNo, Integer pageSize, Coupon param) {
		super();
		this.pageSize = pageSize;
		this.pageNo = pageNo;
		this.param = param;
	}
	
	public void getPageDatas() {
		Page<Coupon,Long> curPage = new Page<Coupon,Long>();
		Integer pageNo = this.getPageNo();
		if (null == pageNo) {
			pageNo = new Integer(1);
		}
		Integer pageSize = this.getPageSize();
		if (null ==pageSize) {
			pageSize = new Integer(10);
		}
		curPage = this.getData(pageNo, pageSize, this.getParam());
		this.setPage(curPage);
	}
	
	private Page<Coupon,Long> getData(int pageNo, int pageSize, Coupon param) {
		ICouponService<Coupon,Long> couponService = (ICouponService<Coupon,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.COUPON_SERVICE_BEAN_NAME);
		couponService.setEntityClass(Coupon.class);
		couponService.setPKClass(Long.class);
		String hql = "from Coupon where 1=1 ";
		Timestamp nowTime = DateTimeUtil.getCurrentTimestamp();
		List paraList = new ArrayList();
		if (null != param) {// only use code type status showOnPage
			
			if (null != param.getStatus() && !("".equals(param.getStatus()))) {
				if (EjdConstants.COUPON_STATUS_ACTIVE.equals(param.getStatus())) {
					hql = hql + " and status=? and availableDate > ? ";
					paraList.add(param.getStatus());
					paraList.add(nowTime);
				} else if (EjdConstants.COUPON_STATUS_USED.equals(param.getStatus())) {
						hql = hql + " and status=? ";
						paraList.add(param.getStatus());
					} else if (EjdConstants.COUPON_STATUS_OVERDUE.equals(param.getStatus())) {
							hql = hql + " and ( status in (?) or availableDate < ?  ) ";
							paraList.add(param.getStatus());
							paraList.add(nowTime);
						}
				
			}
			
			if (null != param.getCustomerId() && !("".equals(param.getCustomerId()))) {
				hql = hql + " and customerId = ? ";
				paraList.add(param.getCustomerId());
			}
		}
		
		Object[] values = new Object[paraList.size()];
		for (int i = 0; i< values.length;i++) {
			values[i] = paraList.get(i);
		}
		Page<Coupon,Long> resultPage = couponService.pagedQuery(hql, pageNo, pageSize,values); 
		return resultPage;
	}
}
